package com.giscloud.limp.service.impl;

import com.giscloud.commons.model.Result;
import com.giscloud.commons.utils.JacksonUtil;
import com.giscloud.commons.utils.StringUtils;
import com.giscloud.limp.dao.CourtInfoDao;
import com.giscloud.limp.entity.CourtInfo;
import com.giscloud.limp.entity.CourtInfoExample;
import com.giscloud.limp.entity.dto.CourtInfoDTO;
import com.giscloud.limp.service.CourtInfoService;
import com.github.pagehelper.PageHelper;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service("courtInfoService")
@Log4j2
public class CourtInfoServiceImpl implements CourtInfoService {
    private final  CourtInfoDao courtInfoDao;

    @Autowired
    public CourtInfoServiceImpl(CourtInfoDao courtInfoDao) {
        this.courtInfoDao = courtInfoDao;
    }

    private CourtInfo.Column[] columns = new CourtInfo.Column[]{CourtInfo.Column.courtId,CourtInfo.Column.courtName, CourtInfo.Column.courtType, CourtInfo.Column.parentId};
    /**
    *
    * 列表
    */
    @Override
    public List<CourtInfo> queryPage(CourtInfoDTO courtInfoDTO) {
        log.debug("#queryPage parameter ={}", JacksonUtil.toJSONString(courtInfoDTO));
        PageHelper.startPage(courtInfoDTO.getPage(), courtInfoDTO.getRows());
        CourtInfoExample courtInfoExample = new CourtInfoExample();
        CourtInfoExample.Criteria criteria = courtInfoExample.createCriteria();
        if(courtInfoDTO.getParentId() != null){
            criteria.andParentIdEqualTo(courtInfoDTO.getParentId());
        }
        if(StringUtils.isNotBlank(courtInfoDTO.getCourtType())){
            criteria.andCourtTypeEqualTo(courtInfoDTO.getCourtType());
        }
        if(StringUtils.isNotBlank(courtInfoDTO.getCourtName())){
            criteria.andCourtNameLike("%"+courtInfoDTO.getCourtName()+"%");
        }
        courtInfoExample.setOrderByClause("disp_order asc");
        List<CourtInfo> courtInfos = courtInfoDao.selectByExampleSelective(courtInfoExample,columns);
        return courtInfos;
    }


    /**
     * 信息
     */
    @Override
    public CourtInfo getById(Integer courtId){
        log.debug("#getById parameter ={}", courtId);
        return courtInfoDao.selectByPrimaryKey(courtId);
    }

    /**
     * 保存
     */
    @Override
    public Result save(CourtInfo courtInfo){
        if(courtInfoDao.insertSelective(courtInfo) > 0 ){
            return Result.success("保存成功");
        }else{
            return Result.error("保存失败");
        }
    }

    /**
     * 修改
     */
    @Override
    public Result updateById(CourtInfo courtInfo){
         CourtInfoExample courtInfoExample = new CourtInfoExample();
         CourtInfoExample.Criteria criteria = courtInfoExample.createCriteria();
         criteria.andCourtIdEqualTo(courtInfo.getCourtId());
         if(courtInfoDao.updateByExampleSelective(courtInfo,courtInfoExample) > 0){
             return Result.success("修改成功");
         }else{
             return Result.error("修改失败");
         }
    }
    /**
     * 删除
     */
    @Override
    public Result removeByIds(List<Integer> courtIds){
        CourtInfoExample courtInfoExample = new CourtInfoExample();
        CourtInfoExample.Criteria criteria = courtInfoExample.createCriteria();

            criteria.andCourtIdIn(courtIds);

        if(courtInfoDao.deleteByExample(courtInfoExample) > 0){
            return Result.success("删除成功");
        }else{
            return Result.error("删除失败");
        }
    }

}